function getUrlParam(name)
{
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r!=null) return unescape(r[2]); return null; //返回参数值
}

$(function () {
    var begin = 0;
    var limit = 20;
    var fetching = false;
    var hasMore = true;
    var topicId = parseInt(getUrlParam('topicid'));
    var noteTemplate = _.template($("#note-manage-template").html());
    var noteDelTemplate = _.template($("#note-del-template").html());
    var winH = $(window).height(); //页面可视区域高度

    function renderNoteList() {
        if (fetching) {
            return;
        }
        fetching = true;

        var noteListUrl = "/api/note/list";
        var data = {
            postData: JSON.stringify({
                idx: 0,
                params: {
                    topicid: topicId,
                    begin: begin,
                    limit: limit
                }
            })
        };

        $.ajax({
            url: noteListUrl,
            data: data,
            type: "post",
            dataType: "json",
            success: function (res) {
                if (res.ret == 0) {
                    $.each(res.res, function (index, noteInfo) {
                        $(".show-container").append(noteTemplate(noteInfo));
                        $("body").append(noteDelTemplate(noteInfo));
                    });
                    begin += limit;
                    if (res.res.length < limit) {
                        hasMore = false;
                    }
                } else {
                    console.log(res);
                }
            },
            error: function (res) {
                console.log(res);
            },
            complete: function () {
                fetching = false;
            }
        });
    }

    $("body").on("click", ".del-note", function(e) {
        var $current = $(e.currentTarget);
        var noteid = $current.data("noteid");

        var url = "/cms/topic/note/del";
        var data = {
            noteid: noteid
        };
        $.ajax({
            url: url,
            data: data,
            type: "post",
            dataType: "json",
            success: function(res) {
                if(res.ret == 0) {
                    $("#manage-note-" + noteid).replaceWith(
    '<div class="alert alert-warning alert-dismissible fade in" role="alert">'
    + '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'
    + '删除成功'
    + '</div>'
                    );
                    $("#del-modal-" + noteid).modal('hide');
                } else {
                    alert(res.msg);
                }
            }
        });
    });

    $("body").on("click", ".edit-n", function(e) {
        e.preventDefault();

        var $current = $(e.currentTarget);
        var noteid = $current.data("noteid");


    });

    $(window).scroll(function () {
        if (!hasMore) {
            return;
        }

        var pageH = $(document.body).height();
        var scrollT = $(window).scrollTop(); //滚动条top
        var aa = (pageH - winH - scrollT) / winH;
        if (aa < 0.02) {
            renderNoteList();
        }
    });

    renderNoteList();

    $(".to-s-note").on("click", function(e) {
        e.preventDefault();

        window.location.href = "/cms2/topic/note?topicid=" + topicId;
    });

    $(".to-p-note").on("click", function(e) {
        e.preventDefault();

        window.location.href = "/cms2/topic/note/publish?topicid=" + topicId;
    });
});